package com.hq;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
    //static Lock lock = new ReentrantLock();
    //static Condition condition = lock.newCondition();
    //Iterator<ScheduleVersionEntity> iterator;
    //Thread thread1;
    //Thread thread2;
    //
    //public ResultMap<AResultCode, String> startMissionTest(String taskId) {
    //    if (CollUtil.isEmpty(futures)) {
    //        thread1 = new Thread(() ->{
    //            scheduleHandle(taskId);},"scheduleHandle");
    //        thread2 = new Thread(() ->{
    //            releaseSchedule();},"releaseSchedule");
    //        thread1.start();
    //        thread2.start();
    //        return ResultMapUtil.ok("任务执行成功");
    //    }
    //    return ResultMapUtil.error("同时只能执行一个任务");
    //};

    //
    //public  void scheduleHandle(String taskId) {
    //    List<ScheduleVersionEntity> ScheduleVersionEntitys = getScheduleVersionData(taskId);
    //    iterator = ScheduleVersionEntitys.iterator();
    //    int sum = 1;
    //    while (true) {
    //        try {
    //            Thread thread = Thread.currentThread();
    //            if(thread1.isInterrupted()){
    //                lock.unlock();
    //                return;
    //            }
    //            lock.lock();
    //            while (iterator.hasNext()) {
    //                if (futures.size() < SystemConfig.CONCURRENT_SUM) {
    //                    log.info("开始测算第{}个任务",sum++);
    //                    ScheduleVersionEntity next = iterator.next();
    //                    iterator.remove();
    //                    Future<?> booleanFuture = threadPoolTaskExecutor.submit(() -> {
    //                        //封装请求参数
    //                        EsRequest esRequest = new EsRequest();
    //                        esRequest.setStartTime(next.getEsBeginTime());
    //                        esRequest.setEndTime(next.getEsEndTime());
    //                        esRequest.setStart(0);
    //                        esRequest.setRows(SystemConfig.SPLIT_DATA_SUM);
    //                        esRequest.setId(next.getId());
    //                        esRequest.setRetryCount(next.getRetryCount());
    //                        esRequest.setFailureName(next.getFailureName());
    //                        String paramVersion = DateUtil.parse(next.getParamVersion(), DatePattern.PURE_DATETIME_PATTERN).toString();
    //                        scheduleVersionService.update(Wrappers.<ScheduleVersionEntity>lambdaUpdate().set(ScheduleVersionEntity::getStatus,1).eq(ScheduleVersionEntity::getId,next.getId()));
    //                        //DateHandlerChain.handle(esRequest, paramVersion + "_" + taskId);
    //                    });
    //                    futures.add(booleanFuture);
    //                } else {
    //                    try {
    //                        condition.await();
    //                    } catch (InterruptedException e) {
    //                        e.printStackTrace();
    //                    }
    //                }
    //            }
    //            List<ScheduleVersionEntity> scheduleVersionData = getScheduleVersionData(taskId);
    //            if(CollUtil.isNotEmpty(scheduleVersionData)){
    //                log.info("获取下一批任务");
    //                iterator = scheduleVersionData.iterator();
    //                continue;
    //            }
    //            isSave(null,taskId);
    //            log.info("执行完了");
    //            futures.clear();
    //            thread1.interrupt();
    //            thread2.interrupt();
    //        } catch (Exception e) {
    //            log.error("任务中断", e.getMessage());
    //        } finally {
    //            lock.unlock();
    //        }
    //    }
    //};
    //
    ///** 释放调度任务 */
    //public  void releaseSchedule(){
    //    while (true){
    //        try {
    //            Thread thread = Thread.currentThread();
    //            if(thread2.isInterrupted()){
    //                lock.unlock();
    //                return;
    //            }
    //            Thread.sleep(2000);
    //            lock.lock();
    //            log.info("监听是否有线程释放");
    //            Iterator<Future> iterator1 = futures.iterator();
    //            while (iterator1.hasNext()) {
    //                Future next = iterator1.next();
    //                if(next.isDone() ||  next.isCancelled()){
    //                    log.info("有线程执行任务完毕 开启新的任务");
    //                    iterator1.remove();
    //                    condition.signal();
    //                }
    //            }
    //        }catch (Exception e){
    //            log.error("任务中断", e.getMessage());
    //        }finally {
    //            lock.unlock();
    //        }
    //    }
    //}


    //public List<ScheduleVersionEntity> getScheduleVersionData(String taskId){
    //    Page<ScheduleVersionEntity> scheduleVersionEntityPage = new Page<>(0, 10, false);
    //    LambdaQueryWrapper<ScheduleVersionEntity> eq = Wrappers.<ScheduleVersionEntity>lambdaQuery()
    //            .eq(ScheduleVersionEntity::getStatus, 0)
    //            .gt(ScheduleVersionEntity::getRetryCount,0)
    //            .eq(ScheduleVersionEntity::getTaskId, taskId);
    //    IPage<ScheduleVersionEntity> page = scheduleVersionService.page(scheduleVersionEntityPage, eq);
    //    List<ScheduleVersionEntity> records = page.getRecords();
    //    return records;
    //}
}
